﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlTypes;

namespace DataAccess.Operate
{
    public class DA_Report_Change_AreaUnit_Operate:SqlInteractionBase
    {
        private DataEntity.T_Report_Change_AreaUnit _objMessage = null;

        public override bool Insert(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Report_Change_AreaUnit_Insert]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = (DataEntity.T_Report_Change_AreaUnit)obj;
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Change_AreaUnit_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_ID));
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Change_AreaUnit_ProjectID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_ProjectID));
                cmdToExecute.Parameters.Add(new SqlParameter("@iReport_Change_AreaUnit_ExchangeTypeID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_ExchangeTypeID));
                cmdToExecute.Parameters.Add(new SqlParameter("@daReport_Change_AreaUnit_ExchangeDate", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_ExchangeDate));
                cmdToExecute.Parameters.Add(new SqlParameter("@daReport_Change_AreaUnit_FormlyerExchangeDate", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_FormlyerExchangeDate));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_AreaUnit_FormlyerReportNO", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_FormlyerReportNO));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_AreaUnit_Unit", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_Unit));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_AreaUnit_FormlyerData", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_FormlyerData));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_AreaUnit_UpdateData", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_UpdateData));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_AreaUnit_ReduceData", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_ReduceData));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_AreaUnit_NowData", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_NowData));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_AreaUnit_AdjustReason", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_AdjustReason));
                cmdToExecute.Parameters.Add(new SqlParameter("@iReport_Plan_Audit", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_Audit));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Report_Change_AreaUnit::Insert::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public override bool Update(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Report_Change_AreaUnit_Update]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = (DataEntity.T_Report_Change_AreaUnit)obj;
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Change_AreaUnit_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_ID));
                cmdToExecute.Parameters.Add(new SqlParameter("@iReport_Change_AreaUnit_ExchangeTypeID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_ExchangeTypeID));
                cmdToExecute.Parameters.Add(new SqlParameter("@daReport_Change_AreaUnit_ExchangeDate", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_ExchangeDate));
                cmdToExecute.Parameters.Add(new SqlParameter("@daReport_Change_AreaUnit_FormlyerExchangeDate", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_FormlyerExchangeDate));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_AreaUnit_FormlyerReportNO", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_FormlyerReportNO));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_AreaUnit_Unit", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_Unit));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_AreaUnit_FormlyerData", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_FormlyerData));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_AreaUnit_UpdateData", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_UpdateData));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_AreaUnit_ReduceData", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_ReduceData));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_AreaUnit_NowData", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_NowData));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_AreaUnit_AdjustReason", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_AreaUnit_AdjustReason));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Report_Change_AreaUnit::Update::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public override bool Delete(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Report_Change_AreaUnit_Delete]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;

            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Change_AreaUnit_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, SqlGuid.Parse(obj.ToString())));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Report_Change_AreaUnit::Delete::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public override object SelectOne(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Report_Change_AreaUnit_SelectOne]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;
            DataTable toReturn = new DataTable("t_Report_Change_AreaUnit");
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = new DataEntity.T_Report_Change_AreaUnit();
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Change_AreaUnit_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, SqlGuid.Parse(obj.ToString())));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                adapter.Fill(toReturn);
                if (toReturn.Rows.Count > 0)
                {
                    this._objMessage.Report_Change_AreaUnit_ID = (Guid)toReturn.Rows[0]["Report_Change_AreaUnit_ID"];
                    this._objMessage.Report_Change_AreaUnit_ProjectID = (Guid)toReturn.Rows[0]["Report_Change_AreaUnit_ProjectID"];
                    //this._objMessage.Report_Change_AreaUnit_ExchangeTypeID = (Int32)toReturn.Rows[0]["Report_Change_AreaUnit_ExchangeTypeID"];
                    this._objMessage.Report_Change_AreaUnit_ExchangeDate = (DateTime)toReturn.Rows[0]["Report_Change_AreaUnit_ExchangeDate"];
                    this._objMessage.Report_Change_AreaUnit_FormlyerExchangeDate = (DateTime)toReturn.Rows[0]["Report_Change_AreaUnit_FormlyerExchangeDate"];
                    this._objMessage.Report_Change_AreaUnit_FormlyerReportNO = (string)toReturn.Rows[0]["Report_Change_AreaUnit_FormlyerReportNO"];
                    this._objMessage.Report_Change_AreaUnit_Unit = (string)toReturn.Rows[0]["Report_Change_AreaUnit_Unit"];
                    this._objMessage.Report_Change_AreaUnit_FormlyerData = (string)toReturn.Rows[0]["Report_Change_AreaUnit_FormlyerData"];
                    this._objMessage.Report_Change_AreaUnit_UpdateData = (string)toReturn.Rows[0]["Report_Change_AreaUnit_UpdateData"];
                    this._objMessage.Report_Change_AreaUnit_ReduceData = (string)toReturn.Rows[0]["Report_Change_AreaUnit_ReduceData"];
                    this._objMessage.Report_Change_AreaUnit_NowData = (string)toReturn.Rows[0]["Report_Change_AreaUnit_NowData"];
                    this._objMessage.Report_Change_AreaUnit_AdjustReason = (string)toReturn.Rows[0]["Report_Change_AreaUnit_AdjustReason"];
                    this._objMessage.Report_Plan_Audit = (Int32)toReturn.Rows[0]["Report_Plan_Audit"];
                }
                return this._objMessage;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Report_Change_AreaUnit::SelectOne::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
                adapter.Dispose();
            }
        }


        public override DataSet SelectAll()
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Report_Change_AreaUnit_SelectAll]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;

            try
            {

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                adapter.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Report_Change_AreaUnit::SelectAll::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
                adapter.Dispose();
            }
        }
    }
}
